#!/usr/bin/env Rscript

rm(list = ls())

setwd("../data/voter_file_aggregated")

## this script calculates summaries for the left table in Figure 3

load("individual_partychanges_summary_beforeaftermoves_nonmovers.RData")
INDIV.partySummary.nonmovers.before <- INDIV.partySummary.movers.before #future movers
INDIV.partySummary.nonmovers.after <- INDIV.partySummary.movers.after #already moved
load("individual_partychanges_summary_beforeaftermoves_movers.RData")

myWeightedMean <- function(changeparty,majorparty,forweights) {
    weights <- forweights / sum(forweights)
    return(weighted.mean(changeparty / majorparty, weights, na.rm=T))
}

myWeightedVar <- function(changeparty,majorparty,forweights) {
    weights <- forweights / sum(forweights)
    my.weighted.sd <- function(p=changeparty/majorparty,n=majorparty,w=weights) {
        (
            sum((w / sum(w * n))^2
             * n * p * (1 - p)
             )
            )
    }
    return(my.weighted.sd())
}

NEVERMOVED08 <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.nevermoved[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nevermoved[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

ALLMOVERS08 <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

NONMOVERS08.before <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.nonmovers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

NONMOVERS08.after <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.nonmovers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

DOUBLEMOVERS08.before <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.movers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.before[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )

DOUBLEMOVERS08.after <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.movers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.after[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[2]],
        2008 - yob09 < 65 & 2008 - yob09 >= 30
        )$majorparty
    )



NEVERMOVED10 <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.nevermoved[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nevermoved[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

ALLMOVERS10 <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

NONMOVERS10.before <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.nonmovers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

NONMOVERS10.after <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.nonmovers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.nonmovers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

DOUBLEMOVERS10.before <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.movers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.before[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

DOUBLEMOVERS10.after <- myWeightedVar(
    changeparty=subset(
        INDIV.partySummary.movers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$changeparty,
    majorparty=subset(
        INDIV.partySummary.movers.after[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty,
    forweights=subset(
        INDIV.partySummary.movers[[3]],
        2010 - yob10 < 65 & 2010 - yob10 >= 30
        )$majorparty
    )

SDs <- t(data.frame(
    nevermoved = sqrt(mean(NEVERMOVED08 + NEVERMOVED10)),
    allmovers = sqrt(mean(ALLMOVERS08 + ALLMOVERS10)),
    nonmovers.before = sqrt(mean(NONMOVERS08.before + NONMOVERS10.before)),
    nonmovers.after = sqrt(mean(NONMOVERS08.after + NONMOVERS10.after)),
    doublemovers.before = sqrt(mean(DOUBLEMOVERS08.before + DOUBLEMOVERS10.before)),
    doublemovers.after = sqrt(mean(DOUBLEMOVERS08.after + DOUBLEMOVERS10.after))
    ))

round(SDs, 4)
